﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YSC_Core.BLL.Domain.BackstageModels;
using YSC_OpenPlatform_API.Entity;
using YSC_Services.BLL.Services.Interfaces;

namespace YSC_Backstage_Web.Controllers
{
    /// <summary>
    /// 产地单位
    /// </summary>
    public class PlaceOfOriginUnitController : Controller
    {
        private readonly IBasisBackstageServices _pro;
        public PlaceOfOriginUnitController(IBasisBackstageServices pro)
        {
            _pro = pro;
        }
        //PlaceOfOriginUnit
        //产地管理
        public IActionResult Index()
        {
            return View();
        }

        public JsonResult GetList(int limit, int page, string keyword)
        {
            var result = _pro.GetPlaceOfOriginUnitAll(page, limit, keyword);
            return Json(result);

        }

        public IActionResult Add()
        {
            return View();
        }

        public IActionResult Edit(string Id)
        {
            var aa = _pro.GetPlaceOfOriginUnit(Id);
            var json = JsonConvert.SerializeObject(aa.Result);
            JObject obj = JObject.Parse(json);
            PlaceOfOriginUnit entity = JsonConvert.DeserializeObject<PlaceOfOriginUnit>(obj["Data"].ToString());//将字符串转回成实体
            return View(entity);
        }

        public JsonResult AddEditSubmit(string paramete, string county, string city, string province)
        {
            PlaceOfOriginUnitModels entity = JsonConvert.DeserializeObject<PlaceOfOriginUnitModels>(paramete);
            entity.AreaUnCode = county != null ? county : city != null ? city : province;
            var result = _pro.AddUpdatePlaceOfOriginUnit(entity);
            return Json("");
        }
    }
}